package com.vpen.iot.utils;

import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;

import java.util.List;

/**
 * 计算两点经纬度距离，单位/米
 */
public class DistanceUtil {
    public static void main(String[] args){
        GlobalCoordinates source = new GlobalCoordinates(29.490295, 106.486654);
        GlobalCoordinates target = new GlobalCoordinates(29.615467, 106.581515);

        double meter1 = getDistanceMeter(source, target, Ellipsoid.Sphere);
        double meter2 = getDistanceMeter(source, target, Ellipsoid.WGS84);
        //计算结果与百度地图一致
        System.out.println("Sphere坐标系计算结果："+meter1 + "米");

        //计算结果与Sphere坐标系存在几十米的误差
        System.out.println("WGS84坐标系计算结果："+meter2 + "米");
    }

    public static double getDistanceMeter(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo, Ellipsoid ellipsoid){
        //创建GeodeticCalculator，调用计算方法，传入坐标系、经纬度用于计算距离
        GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, gpsFrom, gpsTo);
        return geoCurve.getEllipsoidalDistance();
    }

    public static double getTotalDistance(List<GlobalCoordinates> points, Ellipsoid ellipsoid) {
        double totalDistance = 0.0;
        for (int i = 0; i < points.size() - 1; i++) {
            totalDistance += getDistanceMeter(points.get(i), points.get(i + 1), ellipsoid);
        }
        return totalDistance;
    }

    public static double getTotalDistance4(List<GlobalCoordinates> points) {
        double totalDistance = 0.0;
        for (int i = 0; i < points.size() - 1; i++) {
            totalDistance += getDistance4(points.get(i), points.get(i + 1));
        }
        return totalDistance;
    }


    public static Double getDistance4(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo) {
        double latitude1 = gpsFrom.getLatitude();
        double longitude1 = gpsFrom.getLongitude();
        double latitude2 = gpsTo.getLatitude();
        double longitude2 = gpsTo.getLongitude();
        if (longitude1 == 0 || latitude1 == 0 || latitude2 == 0 || longitude2 == 0) {
            return -1.0;
        }
        longitude1 *= 0.01745329251994329;
        latitude1 *= 0.01745329251994329;
        longitude2 *= 0.01745329251994329;
        latitude2 *= 0.01745329251994329;
        double var1 = Math.sin(longitude1);
        double var2 = Math.sin(latitude1);
        double var3 = Math.cos(longitude1);
        double var4 = Math.cos(latitude1);
        double var5 = Math.sin(longitude2);
        double var6 = Math.sin(latitude2);
        double var7 = Math.cos(longitude2);
        double var8 = Math.cos(latitude2);
        double[] var10 = new double[3];
        double[] var20 = new double[3];
        var10[0] = var4 * var3;
        var10[1] = var4 * var1;
        var10[2] = var2;
        var20[0] = var8 * var7;
        var20[1] = var8 * var5;
        var20[2] = var6;

        return Math.asin(Math.sqrt((var10[0] - var20[0]) * (var10[0] - var20[0]) + (var10[1] - var20[1]) * (var10[1] - var20[1]) + (var10[2] - var20[2]) * (var10[2] - var20[2])) / 2.0) * 1.27420015798544E7;
        // 结果四舍五入 保留2位小数
        //return new BigDecimal(distance).setScale(2, RoundingMode.HALF_UP).doubleValue();

    }


}
